home *** CD-ROM | disk | FTP | other *** search
/ The Very Best of Atari Inside / The Very Best of Atari Inside 1.iso / mint / mint110s / dos.c < prev    next >
C/C++ Source or Header  |  1994-02-11  |  13KB  |  606 lines

  1. /*
  2. Copyright 1990,1991,1992 Eric R. Smith.
  3. Copyright 1992 Atari Corporation.
  4. All rights reserved.
  5. */
  6.  
  7. /* miscellaneous DOS functions, and the DOS initialization function */
  8.  
  9. #include "mint.h"
  10.  
  11. #define DOS_MAX 0x140
  12.  
  13. Func dos_tab[DOS_MAX];
  14. short dos_max = DOS_MAX;
  15.  
  16. static void alarmme P_((PROC *));
  17.  
  18. long ARGS_ON_STACK 
  19. s_version()
  20. {
  21.     return Sversion();
  22. }
  23.  
  24. /*
  25.  * Super(new_ssp): change to supervisor mode.
  26.  */
  27.  
  28. long ARGS_ON_STACK
  29. s_uper(new_ssp)
  30.     long new_ssp;
  31. {
  32.     int in_super;
  33.     long r;
  34.  
  35.     TRACE(("Super"));
  36.     in_super = curproc->ctxt[SYSCALL].sr & 0x2000;
  37.  
  38.     if (new_ssp == 1) {
  39.         r = in_super ? -1L : 0;
  40.     }
  41.     else {
  42.         curproc->ctxt[SYSCALL].sr ^= 0x2000;
  43.         r = curproc->ctxt[SYSCALL].ssp;
  44.         if (in_super) {
  45.             if (new_ssp == 0) {
  46.                 DEBUG(("bad Super call"));
  47.                 raise(SIGSYS);
  48.             }
  49.             else {
  50.                 curproc->ctxt[SYSCALL].usp = 
  51.                     curproc->ctxt[SYSCALL].ssp;
  52.                 curproc->ctxt[SYSCALL].ssp = new_ssp;
  53.             }
  54.         }
  55.         else {
  56.             curproc->ctxt[SYSCALL].ssp = 
  57.                 new_ssp ? new_ssp : curproc->ctxt[SYSCALL].usp;
  58.         }
  59.     }
  60.     return r;
  61. }
  62.  
  63. /*
  64.  * get/set time and date functions
  65.  */
  66. long ARGS_ON_STACK t_getdate() { return datestamp; }
  67. long ARGS_ON_STACK t_gettime() { return timestamp; }
  68.  
  69. long ARGS_ON_STACK t_setdate(date)
  70.     int date;
  71. {
  72.     long r;
  73.  
  74. /* Only the superuser may set date or time */
  75.     if (curproc->euid != 0)
  76.         return EACCDN;
  77.     r = Tsetdate(date);
  78.     datestamp = Tgetdate();
  79.     return r;
  80. }
  81.  
  82. long ARGS_ON_STACK t_settime(time)
  83.     int time;
  84. {
  85.     long r;
  86.  
  87.     if (curproc->euid != 0)
  88.         return EACCDN;
  89.     r = Tsettime(time);
  90.     timestamp = Tgettime();
  91.     return r;
  92. }
  93.  
  94. /*
  95.  * GEMDOS extension: Syield(): give up the processor if any other
  96.  * processes are waiting. Always returns 0.
  97.  */
  98.  
  99. long ARGS_ON_STACK
  100. s_yield()
  101. {
  102. /* reward the nice process */
  103.     curproc->curpri = curproc->pri;
  104.     sleep(READY_Q, curproc->wait_cond);
  105.     return 0;
  106. }
  107.  
  108. /*
  109.  * GEMDOS extension:
  110.  * Prenice(pid, delta) sets the process priority level for process pid.
  111.  * A "nice" value < 0 increases priority, one > 0 decreases it.
  112.  * Always returns the new priority (so Prenice(pid, 0) queries the current
  113.  * priority).
  114.  *
  115.  * NOTE: for backward compatibility, Pnice(delta) is provided and is equivalent
  116.  * to Prenice(Pgetpid(), delta)
  117.  */
  118.  
  119. long ARGS_ON_STACK
  120. p_renice(pid, delta)
  121.     int pid, delta;
  122. {
  123.     PROC *p;
  124.  
  125.     if (pid <= 0 || 0 == (p = pid2proc(pid))) {
  126.         return EFILNF;
  127.     }
  128.  
  129.     if (curproc->euid && curproc->euid != p->ruid
  130.         && curproc->ruid != p->ruid) {
  131.         DEBUG(("Prenice: process ownership error"));
  132.         return EACCDN;
  133.     }
  134.     p->pri -= delta;
  135.     if (p->pri < MIN_NICE) p->pri = MIN_NICE;
  136.     if (p->pri > MAX_NICE) p->pri = MAX_NICE;
  137.     p->curpri = p->pri;
  138.     return ((long)p->pri) & 0x0ffff;
  139. }
  140.  
  141. long ARGS_ON_STACK
  142. p_nice(delta)
  143.     int delta;
  144. {
  145.     return p_renice(curproc->pid,delta);
  146. }
  147.  
  148. /*
  149.  * GEMDOS extensions: routines for getting/setting process i.d.'s and
  150.  * user i.d.'s
  151.  */
  152.  
  153. long ARGS_ON_STACK p_getpid() { return curproc->pid; }
  154.  
  155. long ARGS_ON_STACK p_getppid() { return curproc->ppid; }
  156.  
  157. long ARGS_ON_STACK p_getpgrp() { return curproc->pgrp; }
  158.  
  159. /* note: Psetpgrp(0, ...) is equivalent to Psetpgrp(Pgetpid(), ...) */
  160. /* also note: Psetpgrp(x, 0) is equivalent to Psetpgrp(x, x) */
  161.  
  162. long ARGS_ON_STACK p_setpgrp(pid, newgrp)
  163.     int pid, newgrp;
  164. {
  165.     PROC *p;
  166.  
  167.     if (pid == 0)
  168.         p = curproc;
  169.     else if (0 == (p = pid2proc(pid)))
  170.         return EFILNF;
  171.     if ( (curproc->euid) && (p->ruid != curproc->ruid)
  172.           && (p->ppid != curproc->pid) )
  173.         return EACCDN;
  174.  
  175.     if (newgrp < 0)
  176.         return p->pgrp;
  177.  
  178.     if (newgrp == 0)
  179.         newgrp = p->pid;
  180.  
  181.     return (p->pgrp = newgrp);
  182. }
  183.  
  184. long ARGS_ON_STACK p_getuid() { return curproc->ruid; }
  185. long ARGS_ON_STACK p_getgid() { return curproc->rgid; }
  186. long ARGS_ON_STACK p_geteuid() { return curproc->euid; }
  187. long ARGS_ON_STACK p_getegid() { return curproc->egid; }
  188.  
  189. long ARGS_ON_STACK
  190. p_setuid(id)
  191.     int id;
  192. {
  193.     if (curproc->euid == 0 || curproc->ruid == id) {
  194.         curproc->ruid = curproc->euid = id;
  195.         return id;
  196.     }
  197.     return EACCDN;
  198. }
  199.  
  200. long ARGS_ON_STACK
  201. p_setgid(id)
  202.     int id;
  203. {
  204.     if (curproc->euid == 0 || curproc->egid == 0 || curproc->rgid == id) {
  205.         curproc->egid = curproc->rgid = id;
  206.         return id;
  207.     }
  208.     return EACCDN;
  209. }
  210.  
  211. /*
  212.  * a way to get/set process-specific user information. the user information
  213.  * longword is set to "arg", unless arg is -1. In any case, the old
  214.  * value of the longword is returned.
  215.  */
  216.  
  217. long ARGS_ON_STACK
  218. p_usrval(arg)
  219.     long arg;
  220. {
  221.     long r;
  222.  
  223.     TRACE(("Pusrval"));
  224.     r = curproc->usrdata;
  225.     if (arg != -1L)
  226.         curproc->usrdata = arg;
  227.     return r;
  228. }
  229.  
  230. /*
  231.  * set the file creation mask to "mode". Returns the old value of the
  232.  * mask.
  233.  */
  234. long ARGS_ON_STACK p_umask(mode)
  235.     unsigned mode;
  236. {
  237.     long oldmask = curproc->umask;
  238.  
  239.     curproc->umask = mode & (~S_IFMT);
  240.     return oldmask;
  241. }
  242.  
  243. /*
  244.  * get/set the domain of a process. domain 0 is the default (TOS) domain.
  245.  * domain 1 is the MiNT domain. for now, domain affects read/write system
  246.  * calls and filename translation.
  247.  */
  248.  
  249. long ARGS_ON_STACK
  250. p_domain(arg)
  251.     int arg;
  252. {
  253.     long r;
  254.     TRACE(("Pdomain(%d)", arg));
  255.  
  256.     r = curproc->domain;
  257.     if (arg >= 0)
  258.         curproc->domain = arg;
  259.     return r;
  260. }
  261.  
  262. /*
  263.  * get process resource usage. 8 longwords are returned, as follows:
  264.  *     r[0] == system time used by process
  265.  *     r[1] == user time used by process
  266.  *     r[2] == system time used by process' children
  267.  *     r[3] == user time used by process' children
  268.  *     r[4] == memory used by process
  269.  *     r[5] - r[7]: reserved for future use
  270.  */
  271.  
  272. long ARGS_ON_STACK
  273. p_rusage(r)
  274.     long *r;
  275. {
  276.     r[0] = curproc->systime;
  277.     r[1] = curproc->usrtime;
  278.     r[2] = curproc->chldstime;
  279.     r[3] = curproc->chldutime;
  280.     r[4] = memused(curproc);
  281.     return 0;
  282. }
  283.  
  284. /*
  285.  * get/set resource limits i to value v. The old limit is always returned;
  286.  * if v == -1, the limit is unchanged, otherwise it is set to v. Possible
  287.  * values for i are:
  288.  *    1:  max. cpu time    (milliseconds)
  289.  *    2:  max. core memory allowed
  290.  *    3:  max. amount of malloc'd memory allowed
  291.  */
  292. long ARGS_ON_STACK
  293. p_setlimit(i, v)
  294.     int i;
  295.     long v;
  296. {
  297.     long oldlimit;
  298.  
  299.     switch(i) {
  300.     case 1:
  301.         oldlimit = curproc->maxcpu;
  302.         if (v >= 0) curproc->maxcpu = v;
  303.         break;
  304.     case 2:
  305.         oldlimit = curproc->maxcore;
  306.         if (v >= 0) {
  307.             curproc->maxcore = v;
  308.             recalc_maxmem(curproc);
  309.         }
  310.         break;
  311.     case 3:
  312.         oldlimit = curproc->maxdata;
  313.         if (v >= 0) {
  314.             curproc->maxdata = v;
  315.             recalc_maxmem(curproc);
  316.         }
  317.         break;
  318.     default:
  319.         DEBUG(("Psetlimit: invalid mode %d", i));
  320.         return EINVFN;
  321.     }
  322.     TRACE(("p_setlimit(%d, %ld): oldlimit = %ld", i, v, oldlimit));
  323.     return oldlimit;
  324. }
  325.  
  326. /*
  327.  * pause: just sleeps on IO_Q, with wait_cond == -1. only a signal will
  328.  * wake us up
  329.  */
  330.  
  331. long ARGS_ON_STACK
  332. p_pause()
  333. {
  334.     TRACE(("Pause"));
  335.     sleep(IO_Q, -1L);
  336.     return 0;
  337. }
  338.  
  339. /*
  340.  * helper function for t_alarm: this will be called when the timer goes
  341.  * off, and raises SIGALRM
  342.  */
  343.  
  344. static void
  345. alarmme(p)
  346.     PROC *p;
  347. {
  348.     p->alarmtim = 0;
  349.     post_sig(p, SIGALRM);
  350. }
  351.  
  352. /*
  353.  * t_alarm(x): set the alarm clock to go off in "x" seconds. returns the
  354.  * old value of the alarm clock
  355.  */
  356.  
  357. long ARGS_ON_STACK
  358. t_alarm(x)
  359.     long x;
  360. {
  361.     long oldalarm;
  362.     oldalarm = t_malarm(x*1000);
  363.     oldalarm = (oldalarm+999)/1000;        /* convert to seconds */
  364.     return oldalarm;
  365. }
  366.  
  367. /*
  368.  * t_malarm(x): set the alarm clock to go off in "x" milliseconds. returns
  369.  * the old value ofthe alarm clock
  370.  */
  371.  
  372. long ARGS_ON_STACK
  373. t_malarm(x)
  374.     long x;
  375. {
  376.     long oldalarm;
  377.     TIMEOUT *t;
  378.  
  379. /* see how many milliseconds there were to the alarm timeout */
  380.     oldalarm = 0;
  381.  
  382.     if (curproc->alarmtim) {
  383.         for (t = tlist; t; t = t->next) {
  384.             oldalarm += t->when;
  385.             if (t == curproc->alarmtim)
  386.                 goto foundalarm;
  387.         }
  388.         DEBUG(("Talarm: old alarm not found!"));
  389.         oldalarm = 0;
  390.         curproc->alarmtim = 0;
  391. foundalarm:
  392.         ;
  393.     }
  394.  
  395. /* we were just querying the alarm */
  396.     if (x < 0)
  397.         return oldalarm;
  398.  
  399. /* cancel old alarm */
  400.     if (curproc->alarmtim)
  401.         canceltimeout(curproc->alarmtim);
  402.  
  403. /* add a new alarm, to occur in x milliseconds */
  404.     if (x)
  405.         curproc->alarmtim = addtimeout(x, alarmme);
  406.     else
  407.         curproc->alarmtim = 0;
  408.  
  409.     return oldalarm;
  410. }
  411.  
  412. /*
  413.  * sysconf(which): returns information about system configuration.
  414.  * "which" specifies which aspect of the system configuration is to
  415.  * be returned:
  416.  *    -1    max. value of "which" allowed
  417.  *    0    max. number of memory regions per proc
  418.  *    1    max. length of Pexec() execution string {ARG_MAX}
  419.  *    2    max. number of open files per process    {OPEN_MAX}
  420.  *    3    number of supplementary group id's    {currently 0}
  421.  *    4    max. number of processes per uid    {CHILD_MAX}
  422.  *
  423.  * unlimited values (e.g. CHILD_MAX) are returned as 0x7fffffffL
  424.  *
  425.  * See also Dpathconf() in dosdir.c.
  426.  */
  427.  
  428. long ARGS_ON_STACK
  429. s_ysconf(which)
  430.     int which;
  431. {
  432.     if (which == -1)
  433.         return 4;
  434.  
  435.     switch(which) {
  436.         case 0:
  437.             return UNLIMITED;
  438.         case 1:
  439.             return 126;
  440.         case 2:
  441.             return MAX_OPEN;
  442.         case 3:
  443.             return 0;
  444.         case 4:
  445.             return UNLIMITED;
  446.         default:
  447.             return EINVFN;
  448.     }
  449. }
  450.  
  451. /*
  452.  * Salert: send an ALERT message to the user, via the same mechanism
  453.  * the kernel does (i.e. u:\pipe\alert, if it's available
  454.  */
  455.  
  456. long ARGS_ON_STACK
  457. s_alert(str)
  458.     char *str;
  459. {
  460. /* how's this for confusing code? _ALERT tries to format the
  461.  * string as an alert box; if it fails, we let the full-fledged
  462.  * ALERT function (which will try _ALERT, and fail again)
  463.  * print the alert to the debugging device
  464.  */
  465.     if (_ALERT(str) == 0)
  466.         ALERT(str);
  467.     return 0;
  468. }
  469.  
  470. /*
  471.  * routine for initializing DOS
  472.  *
  473.  * NOTE: before adding new functions, check the definition of
  474.  * DOS_MAX at the top of this file to make sure that there
  475.  * is room; if not, increase DOS_MAX.
  476.  */
  477.  
  478. void
  479. init_dos()
  480. {
  481. /* miscellaneous initialization goes here */
  482.     timestamp = Tgettime();
  483.     datestamp = Tgetdate();
  484.  
  485. /* dos table initialization */
  486.     dos_tab[0x00] = p_term0;
  487.     dos_tab[0x01] = c_conin;
  488.     dos_tab[0x02] = c_conout;
  489.     dos_tab[0x03] = c_auxin;
  490.     dos_tab[0x04] = c_auxout;
  491.     dos_tab[0x05] = c_prnout;
  492.     dos_tab[0x06] = c_rawio;
  493.     dos_tab[0x07] = c_rawcin;
  494.     dos_tab[0x08] = c_necin;
  495.     dos_tab[0x09] = c_conws;
  496.     dos_tab[0x0a] = c_conrs;
  497.     dos_tab[0x0b] = c_conis;
  498.     dos_tab[0x0e] = d_setdrv;
  499.     dos_tab[0x10] = c_conos;
  500.     dos_tab[0x11] = c_prnos;
  501.     dos_tab[0x12] = c_auxis;
  502.     dos_tab[0x13] = c_auxos;
  503.     dos_tab[0x14] = m_addalt;
  504.     dos_tab[0x15] = s_realloc;
  505.     dos_tab[0x19] = d_getdrv;
  506.     dos_tab[0x1a] = f_setdta;
  507.     dos_tab[0x20] = s_uper;
  508.     dos_tab[0x2a] = t_getdate;
  509.     dos_tab[0x2b] = t_setdate;
  510.     dos_tab[0x2c] = t_gettime;
  511.     dos_tab[0x2d] = t_settime;
  512.     dos_tab[0x2f] = f_getdta;
  513.     dos_tab[0x30] = s_version;
  514.     dos_tab[0x31] = p_termres;
  515.     dos_tab[0x36] = d_free;
  516.     dos_tab[0x39] = d_create;
  517.     dos_tab[0x3a] = d_delete;
  518.     dos_tab[0x3b] = d_setpath;
  519.     dos_tab[0x3c] = f_create;
  520.     dos_tab[0x3d] = f_open;
  521.     dos_tab[0x3e] = f_close;
  522.     dos_tab[0x3f] = f_read;
  523.     dos_tab[0x40] = f_write;
  524.     dos_tab[0x41] = f_delete;
  525.     dos_tab[0x42] = f_seek;
  526.     dos_tab[0x43] = f_attrib;
  527.     dos_tab[0x44] = m_xalloc;
  528.     dos_tab[0x45] = f_dup;
  529.     dos_tab[0x46] = f_force;
  530.     dos_tab[0x47] = d_getpath;
  531.     dos_tab[0x48] = m_alloc;
  532.     dos_tab[0x49] = m_free;
  533.     dos_tab[0x4a] = m_shrink;
  534.     dos_tab[0x4b] = p_exec;
  535.     dos_tab[0x4c] = p_term;
  536.     dos_tab[0x4e] = f_sfirst;
  537.     dos_tab[0x4f] = f_snext;
  538.     dos_tab[0x56] = f_rename;
  539.     dos_tab[0x57] = f_datime;
  540.     dos_tab[0x5c] = f_lock;
  541.  
  542. /* MiNT extensions to GEMDOS */
  543.  
  544.     dos_tab[0xff] = s_yield;
  545.     dos_tab[0x100] = f_pipe;
  546.     dos_tab[0x104] = f_cntl;
  547.     dos_tab[0x105] = f_instat;
  548.     dos_tab[0x106] = f_outstat;
  549.     dos_tab[0x107] = f_getchar;
  550.     dos_tab[0x108] = f_putchar;
  551.     dos_tab[0x109] = p_wait;
  552.     dos_tab[0x10a] = p_nice;
  553.     dos_tab[0x10b] = p_getpid;
  554.     dos_tab[0x10c] = p_getppid;
  555.     dos_tab[0x10d] = p_getpgrp;
  556.     dos_tab[0x10e] = p_setpgrp;
  557.     dos_tab[0x10f] = p_getuid;
  558.     dos_tab[0x110] = p_setuid;
  559.     dos_tab[0x111] = p_kill;
  560.     dos_tab[0x112] = p_signal;
  561.     dos_tab[0x113] = p_vfork;
  562.     dos_tab[0x114] = p_getgid;
  563.     dos_tab[0x115] = p_setgid;
  564.  
  565.     dos_tab[0x116] = p_sigblock;
  566.     dos_tab[0x117] = p_sigsetmask;
  567.     dos_tab[0x118] = p_usrval;
  568.     dos_tab[0x119] = p_domain;
  569.     dos_tab[0x11a] = p_sigreturn;
  570.     dos_tab[0x11b] = p_fork;
  571.     dos_tab[0x11c] = p_wait3;
  572.     dos_tab[0x11d] = f_select;
  573.     dos_tab[0x11e] = p_rusage;
  574.     dos_tab[0x11f] = p_setlimit;
  575.     dos_tab[0x120] = t_alarm;
  576.     dos_tab[0x121] = p_pause;
  577.     dos_tab[0x122] = s_ysconf;
  578.     dos_tab[0x123] = p_sigpending;
  579.     dos_tab[0x124] = d_pathconf;
  580.     dos_tab[0x125] = p_msg;
  581.     dos_tab[0x126] = f_midipipe;
  582.     dos_tab[0x127] = p_renice;
  583.     dos_tab[0x128] = d_opendir;
  584.     dos_tab[0x129] = d_readdir;
  585.     dos_tab[0x12a] = d_rewind;
  586.     dos_tab[0x12b] = d_closedir;
  587.     dos_tab[0x12c] = f_xattr;
  588.     dos_tab[0x12d] = f_link;
  589.     dos_tab[0x12e] = f_symlink;
  590.     dos_tab[0x12f] = f_readlink;
  591.     dos_tab[0x130] = d_cntl;
  592.     dos_tab[0x131] = f_chown;
  593.     dos_tab[0x132] = f_chmod;
  594.     dos_tab[0x133] = p_umask;
  595.     dos_tab[0x134] = p_semaphore;
  596.     dos_tab[0x135] = d_lock;
  597.     dos_tab[0x136] = p_sigpause;
  598.     dos_tab[0x137] = p_sigaction;
  599.     dos_tab[0x138] = p_geteuid;
  600.     dos_tab[0x139] = p_getegid;
  601.     dos_tab[0x13a] = p_waitpid;
  602.     dos_tab[0x13b] = d_getcwd;
  603.     dos_tab[0x13c] = s_alert;
  604.     dos_tab[0x13d] = t_malarm;
  605. }
  606.